package com.family.pedigree.service.impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import com.family.common.core.oss.OssClient;
import com.family.common.core.oss.OssFactory;
import com.family.common.core.oss.UploadResult;
import com.family.common.exception.ServiceException;
import com.family.common.utils.StringUtils;
import com.family.common.utils.file.FileUtils;
import com.family.pedigree.domain.FamilyOss;
import com.family.pedigree.mapper.FamilyOssMapper;
import com.family.pedigree.service.IFamilyOssService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;

/**
 * <p>
 * 文件管理表 服务实现类
 * </p>
 *
 * @author forever
 * @since 2024-11-24
 */
@Service
@RequiredArgsConstructor
public class FamilyOssServiceImpl extends ServiceImpl<FamilyOssMapper, FamilyOss> implements IFamilyOssService {


    @Override
    public UploadResult upload(MultipartFile file) {

        OssClient client = OssFactory.instance();
        String originalfileName = file.getOriginalFilename();
        String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
        String prefix = StringUtils.substring(originalfileName, 0, originalfileName.lastIndexOf(".")) + "_" + System.currentTimeMillis();
        UploadResult result = null;
        try {
            result = client.uploadSuffix(file.getBytes(), prefix, suffix, file.getContentType());

            FamilyOss oss = new FamilyOss();
            oss.setFileName(prefix)
                    .setOriginalName(originalfileName)
                    .setFileSuffix(suffix)
                    .setUrl(result.getUrl())
                    .setCreateTime(LocalDateTime.now())
                    .setUpdateBy("admin")
                    .setUpdateTime(LocalDateTime.now())
                    .setCreateBy("admin");
            save(oss);
            result.setOssId(oss.getOssId());

        } catch (IOException e) {
            throw new ServiceException(e.getMessage());
        }

        return result;
    }

    @Override
    public void download(Long ossId, HttpServletResponse response) throws IOException {
        FamilyOss oss = getById(ossId);
        if (ObjectUtil.isNull(oss)) {
            throw new ServiceException("文件数据不存在!");
        }
        FileUtils.setAttachmentResponseHeader(response, oss.getOriginalName());
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
        OssClient storage = OssFactory.instance();
        try (InputStream inputStream = storage.getObjectContent(oss.getUrl())) {
            int available = inputStream.available();
            IoUtil.copy(inputStream, response.getOutputStream(), available);
            response.setContentLength(available);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public void delById(Long ossId) {
        FamilyOss oss = getById(ossId);
        if (ObjectUtil.isNull(oss)) return;

        OssClient instance = OssFactory.instance();
        instance.delete(oss.getUrl());
        removeById(ossId);
    }
}
